package com.zzz.system.controller;

import com.zzz.framework.model.AmisSource;
import com.zzz.framework.model.PageResult;
import com.zzz.framework.model.R;
import com.zzz.system.entity.TemplateGroup;
import com.zzz.system.service.TemplateGroupService;
import com.zzz.system.vo.QueryTemplateGroupVO;
import com.zzz.system.vo.SaveTemplateGroupVO;
import com.zzz.system.vo.TemplateGroupVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("api")
public class TemplateGroupController {

    @Autowired
    private TemplateGroupService templateGroupService;

    @GetMapping("templateGroup/listSelect")
    public R<AmisSource> listSelect() {
        return R.data(AmisSource.of(templateGroupService.list(), TemplateGroup::getCode, TemplateGroup::getCode));
    }

    @PostMapping("templateGroup/add")
    public R add(@RequestBody @Validated @NotNull SaveTemplateGroupVO saveTemplateGroupVO) {
        return templateGroupService.add(saveTemplateGroupVO);
    }

    @PostMapping("templateGroup/findByCondition")
    public R<PageResult<TemplateGroupVO>> findByCondition(@RequestBody @NotNull @Validated QueryTemplateGroupVO queryVO) {
        return R.data(templateGroupService.findByCondition(queryVO));
    }

    @GetMapping("templateGroup/findByCode")
    public R<TemplateGroupVO> findByCode(@Param("code") String code) {
        if (StringUtils.isBlank(code)) {
            return R.data(null);
        }
        return R.data(templateGroupService.findByCode(code));
    }

    @PostMapping("templateGroup/remove/{id}")
    public R remove(@PathVariable Long id) {
        return templateGroupService.remove(id);
    }

    @PostMapping("templateGroup/copy/{id}")
    public R copy(@PathVariable Long id, @RequestBody @Validated SaveTemplateGroupVO saveTemplateGroupVO) {
        return templateGroupService.copy(id, saveTemplateGroupVO);
    }
}
